﻿define(function (require) {
    var service = require('utility/service');

    var workitemStatus = {
        Ongoing: 0,  // 正在进行中
        Completed: 1,  // 已经完成
        Expired: 2,  // 过期了
        WaitingForConfirm: 3,  // 等待确认
        Ready: 4  // 没有负责人，等待分配
    };

    var commandValue = {
        finish: { commandText: '完成', commandClass: 'green', commandOrder: 2 },
        confirm: { commandText: '确认', commandClass: 'blue', commandOrder: 1 },
        takeOwnerShip: { commandText: '负责', commandClass: 'orange', commandOrder: 3},
        delay: { commandText: '完成', commandClass: 'rosy', commandOrder: 0 },
        none: { commandText: '', commandClass: '',commandOrder: 4 },
    };

    window.Workitem = Backbone.Model.extend({
        initialize: function () {
            this.setOwnerAndPlanOn();
            this.setAction();
        },
        setOwnerAndPlanOn: function () {
            var ownerNameAndPlanOn = "";
            
            if (this.get('PlanOn') || this.get('OwnerName')) {
                var ownerName = this.get('OwnerName') ? this.get('OwnerName') : "";
                var planOnStr = this.get('PlanOn') ? Date.fromEpoch(this.get('PlanOn')).toShortDate() : "";

                ownerNameAndPlanOn = "(" + ownerName + "&nbsp;" + planOnStr + ")";
            }

            this.set({ 'OwnerNameAndPlanOn': ownerNameAndPlanOn });
        },
        setAction: function () {
            var currentUser = service.GetCurrentUser_CacheSync()
            var status = this.get("StatusValue");

            switch (status) {
                case workitemStatus.Expired:
                    {
                        this.setActionValue('delay');
                        break;
                    }
                case workitemStatus.WaitingForConfirm:
                    {
                        this.get('OwnerId') == currentUser.Id ?
                            this.setActionValue('confirm') : this.setActionValue('none')
                        break;
                    }
                case workitemStatus.Ready:
                    {
                        this.setActionValue('takeOwnerShip');
                        break;
                    }
                case workitemStatus.Ongoing:
                    {
                        this.setActionValue('finish');
                        break;
                    }
            }
        },
        setActionValue: function (type) {
            this.set({ 
                       "CommandText": commandValue[type].commandText,
                       "CommandClass": commandValue[type].commandClass,
                       "Order": commandValue[type].commandOrder
                    });
        },
        finish: function (e) {
            //todo:
        },
        confirm: function (e) {
            //todo:
        },
        takeOwnerShip: function (e) {
            //todo:
        }
    });

});
